import Vue from 'vue'
import VueRouter from 'vue-router'
import layOut from '@/views/layout/layOut'
import home from '@/views/layout/homePage'
import mine from '@/views/layout/minePage'
import searchPage from '@/views/layout/searchPage'
import postArticle from '@/views/post/postArticle'
import aDetail from '@/views/layout/aDetail'
import edifyPage from '@/views/post/edifyPage'
import noRight from '@/views/noRight/noRight'
import notFound from '@/views/notFound/notFound'
import store from '@/store'
Vue.use(VueRouter)




const routes = [
  {
    path:'/',
    component:layOut,
    redirect:'/home',
    children:[
      {
        path:'/home',
        component:home
      },
      {
        path:'/mine',
        component:mine
      },
      {
        path:'/search',
        component: searchPage
      },
     
    ]
  },
  {path:'/post',component:postArticle},
  { path: '/edify', component: edifyPage },
  {
    path: '/detail',
    component: aDetail
  },
  {
    path: '/noRight',
    component: noRight
  },
  {
    path: '*',
    component: notFound
  },
  
]



const router = new VueRouter({
  routes
})


const authUrl = ['/mine', '/post','/edify']
router.beforeEach((to, from, next) => {
  // const uid = store.state.user.userInf.uid
  const admin = store.state.user.userInf.admin
  if (!authUrl.includes(to.path)) {
    next()
    return
  }

  if (admin) {
    next()
  } else {
    next('/noRight')
  }
})

export default router
